/**
 *
 */
package com.iot.ic.modules.web;

import com.iot.ic.common.web.BaseController;
import com.iot.ic.modules.model.User;
import com.iot.ic.modules.model.UserBank;
import com.iot.ic.modules.service.BankService;
import com.iot.ic.modules.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 */
@Controller
public class BankController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private BankService bankService;

    @RequestMapping("/patient/addBank")
    @ResponseBody
    public Map<String, Object> patientAddBank(UserBank bank) {
        return addBank(bank, 0);
    }

    @RequestMapping("/doctor/addBank")
    @ResponseBody
    public Map<String, Object> doctorAddBank(UserBank bank) {
        return addBank(bank, 1);
    }

    @RequestMapping("/patient/findBank")
    @ResponseBody
    public Map<String, Object> patientFindBank(Integer id,@RequestParam(value = "userId",required = false) Integer userId,
                                               String bankNo) {
        return findBank(id, userId, bankNo);
    }

    @RequestMapping("/doctor/findBank")
    @ResponseBody
    public Map<String, Object> doctorFindBank(Integer id, Integer userId, String bankNo) {
        return findBank(id, userId, bankNo);
    }

    private Map<String, Object> addBank(UserBank bank, Integer i) {
        if (StringUtils.isEmpty(bank.getUserId())) {
            return packInfo(false, "用户编号，保存", "userId");
        }
        if (StringUtils.isEmpty(bank.getBankName())) {
            return packInfo(false, "请选择银行种类，保存", "bankName");
        }
        if (StringUtils.isEmpty(bank.getBankCardNo())) {
            return packInfo(false, "请输入银行卡号码，保存", "bankCardNo");
        }
        if (StringUtils.isEmpty(bank.getBankAddress())) {
            return packInfo(false, "银行地址为空，保存", "bankAddress");
        }
        if (StringUtils.isEmpty(bank.getBankAddress())) {
            return packInfo(false, "银行地址为空，保存", "bankAddress");
        }
        try {
            User user = userService.findById(bank.getUserId(), i);
            if (user == null) {
                return packInfo(false, "用户不存在，保存");
            }
            UserBank oldBank = bankService.findBankByNo(bank.getBankCardNo());
            if (oldBank != null) {
                return packInfo(false, "该银行卡已被绑定，保存");
            }
            bank.setAddtime(new Date());
            UserBank ub = bankService.addBank(bank);
            return packSaveInfo(ub);
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }

    private Map<String, Object> findBank(Integer id, Integer userId, String bankNo) {
        try {
            List<UserBank> patient = bankService.findBank( id,  userId,  bankNo);
            return packData(patient);
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }

}
